
# hello, a comment here
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
import sys

if __name__ == '__main__':
    db = create_engine('mssql://user_cm_temp:Hyth3#4%sHnbvCdxUP9kndhSw@SQL08DEV1:1433/CM', echo = False)
    metadata = MetaData(db)
    metadata.reflect()

    arg = ''
    if len(sys.argv) > 1:
        arg = sys.argv[1].lower()

    if arg == 'tbl-index':

        for table in metadata.sorted_tables:
            print table.name
        
        tables = ['tblCMMaster', 'OtherInfo', 'Flow', 'tblPhotos']
        for tbl in tables:
            curr_table = Table(tbl, metadata, autoload = True)
            print '\nTable: ' + tbl
        
            print '\ncolumns:'
            for col in curr_table.columns:
                print col.name
        
            print '\nprim key columns:'
            for primary_key in curr_table.primary_key:
                print primary_key
        
            print '\nforeign key columns:'
            for fkey in curr_table.foreign_keys:
                print fkey

    elif arg == 'test-query':
        Session = sessionmaker(bind=db)
        session = Session()

        #cm_master = Table('tblCMMaster', metadata, autoload = True, autoload_with = db)
        cm_master = metadata.tables['tblCMMaster']
        flow = Table('Flow', metadata, autoload = True, autoload_with = db)
        other_info = Table('OtherInfo', metadata, autoload = True, autoload_with = db)

        q = session.query(cm_master)
        #records = q.filter(cm_master.columns.ID == 2105005)
        records = q.filter('ID=2105005')

        
        print cm_master.columns
        for rec in records:
            print rec.

